package ru.usu.gv.gui;

import ru.usu.gv.utils.Files;

import javax.swing.JComponent;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.filechooser.FileFilter;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Window;

/**
 * @author spupyrev
 * 30.10.2008
 */
public class GUIUtilities
{
	private static JFileChooser openSaveFileChooser;
	private static JFileChooser saveAsFileChooser;

	public static String chooseOpenSaveFile(Component parent, boolean isOpenDialog)
	{
		JFileChooser chooser = getOpenSaveJFileChooser();

		int returnVal = (isOpenDialog ? chooser.showOpenDialog(parent) : chooser.showSaveDialog(parent));
		if (returnVal == JFileChooser.APPROVE_OPTION)
		{
			return chooser.getSelectedFile().getAbsolutePath();
		}

		return null;
	}

	public static String chooseSaveAsFile(Component parent)
	{
		JFileChooser chooser = getSaveAsJFileChooser();

		int returnVal = chooser.showSaveDialog(parent);
		if (returnVal == JFileChooser.APPROVE_OPTION)
		{
			return chooser.getSelectedFile().getAbsolutePath();
		}

		return null;
	}

	public static JFileChooser getOpenSaveJFileChooser()
	{
		if (openSaveFileChooser == null)
		{
			openSaveFileChooser = new JFileChooser(getStartDirectory());
			openSaveFileChooser.setFileFilter(Files.getFileFilter());
			openSaveFileChooser.setAcceptAllFileFilterUsed(false);
			openSaveFileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
		}

		return openSaveFileChooser;
	}

	public static JFileChooser getSaveAsJFileChooser()
	{
		if (saveAsFileChooser == null)
		{
			saveAsFileChooser = new JFileChooser(getStartDirectory());

			for (FileFilter fileFilter : Files.listExportFileFilters())
				saveAsFileChooser.addChoosableFileFilter(fileFilter);

			saveAsFileChooser.setAcceptAllFileFilterUsed(false);
			saveAsFileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
		}

		return saveAsFileChooser;
	}

	private static String getStartDirectory()
	{
		return System.getProperty("user.dir") + "/resources";
	}

	public static void centralizeFrame(Window component)
	{
		final JFrame mainFrame = GUIEngine.getInstance().getMainFrame();

		int barWidth = component.getWidth();
		int barHeight = component.getHeight();
		int screenWidth = mainFrame.getWidth();
		int screenHeight = mainFrame.getHeight();

		component.setLocation(screenWidth / 2 - barWidth / 2, screenHeight / 2 - barHeight / 2);
	}

	public static JPanel wrapComponentByPanel(JComponent component)
	{
		JPanel panel = new JPanel(new BorderLayout());
		panel.add(component, BorderLayout.WEST);
		return panel;
	}

	/**
	 * Resize component and align it to the left
	 */
	public static JComponent prepareComponent(JComponent component, int width, int height)
	{
		component.setPreferredSize(new Dimension(width, height));
		component.setAlignmentX(0.0f);
		return component;
	}
}
